package net.oov.identify.model;

import net.oov.identify.Buck;
import net.oov.identify.SequenceFreqContainer;
import net.oov.identify.StatusTree.TreeNode;
import net.oov.identify.util.CharExtUtil;

public class MutualInfoExtModel extends CheckModelAdapter{
	
	private SequenceFreqContainer freqContainer;
	private MutualInfoModel mim = null;
	private Double dValue = null;
	public Double miValueL = null;
	public Double miValueR = null;
	
	public MutualInfoExtModel(SequenceFreqContainer freqContainer, Double dValue, MutualInfoModel mim){
		super();
		this.dValue = dValue;
		this.freqContainer = freqContainer;
		this.mim = mim;
	}
	
	public Boolean check(StatusNode ct, CharSequence cs){
		
		miValueL = null;
		miValueR = null;
		
		Buck buck1 = new Buck(freqContainer.getDicReverse());
		
		Character[] cc = new Character[1];
		TreeNode ctL = buck1.getNextNodeWithMaxFreq(CharExtUtil.reverse(cs.toString()), cc);
		if(ctL!=null){//左结合率
			miValueL = mim.value(ctL, String.valueOf(cc[0]),cs);
//			miValueL = this.value(ctL, String.valueOf(cc[0]),cs.subSequence(0, 1));//用整个串进行互信息计算更有意义
			Double ddValue = dValue;
			if(ctL.freq() > ct.freq()*0.618){//0.618为黄金分割点,加大差值限制
				ddValue = dValue*(1+ Double.valueOf(ctL.freq())/(ct.freq() - ctL.freq()));
			}
			if(mim.value()<miValueL+ddValue){
//				wl(cs+"，左 ， "+mim.value()+"<"+miValueL+"+"+ddValue);
				return Boolean.FALSE;
			}

		}

		Buck buck2 = new Buck(freqContainer.getDicForward());
		TreeNode ctR = buck2.getNextNodeWithMaxFreq(cs.toString(),cc);
		if(ctR!=null){//右结合率
			miValueR = mim.value(ctR, cs.toString(),String.valueOf(cc[0]));
//			miValueR = mim.value(ctR, cs.subSequence(0, 1),String.valueOf(cc[0]));
			Double ddValue = dValue;
			if(ctR.freq() > ct.freq()*0.618){//0.618为黄金分割点
				ddValue = dValue*(1+ Double.valueOf(ctR.freq())/(ct.freq() - ctR.freq()));
			}
			if(mim.value()<miValueR+ddValue){
//				wl(cs+"，右 ， "+mim.value()+"<"+miValueR+"+"+ddValue);
				return Boolean.FALSE;
			}
		}
		return Boolean.TRUE;
	}
	

	
}
